/*
 * MIT License
 *
 * Copyright (c) 2020 wen.gu <454727014@qq.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

/***************************************************************************
 * Name: cbus_message_manager.h
 *
 * Purpose: implementation message manager for cbus endpoint
 *
 * Developer:
 *   wen.gu , 2022-02-16
 *
 * TODO:
 *
 ***************************************************************************/

/******************************************************************************
 **    INCLUDES
 ******************************************************************************/
#ifndef __CBUS_MESSAGE_MANAGER_H__
#define __CBUS_MESSAGE_MANAGER_H__

#include "cbus/core/cbus_type.h"
#include "cbus/core/cbus_error.h"
#include "cbus/bus/cbus_message.h"
/******************************************************************************
 **    MACROS
 ******************************************************************************/

/******************************************************************************
 **    TYPE DEFINITIONS
 ******************************************************************************/
G_C_HEADER_START

typedef void (*cbus_message_on_process_func)(cbus_message_t request_message);


typedef struct _cbus_message_handler
{
    GPTR opaque;
    cbus_message_on_process_func on_message;
}cbus_message_handler_t;

/** a virtual group to register message, haven't really group id field in data struct of message define */
typedef struct _cbus_message_group 
{
    cbus_message_type_t message_type; /** indicate that: all message in a group must be have the same 'message type' */
    //GBOL independent_worker; /** GTRUE: endpoint will be alloc a independent worker thread to proccess this message group, GFALSE: will not */
    GU32 size; /** how many message in current group */
    cbus_message_id* message_ids; /** message id array */

    GPTR opaque;
    cbus_message_on_process_func on_message;
}cbus_message_group_t;

typedef struct _cbus_message_info
{
    cbus_message_type_t message_type;
    cbus_message_id message_id;
    GPTR opaque;
    cbus_message_on_process_func on_message;
}cbus_message_info_t;

typedef GPTR cbus_message_manager_t;
/******************************************************************************
 **    CLASSES/FUNCTIONS DEFINITIONS
 ******************************************************************************/


G_API cbus_message_manager_t cbus_message_manager_create();
G_API void cbus_message_manager_destroy(cbus_message_manager_t manager);

G_API GErrc cbus_message_manager_register(cbus_message_manager_t manager, const cbus_message_info_t* msg_info);
G_API GErrc cbus_message_manager_register_group(cbus_message_manager_t manager, const cbus_message_group_t* group);
G_API GErrc cbus_message_manager_register_category(cbus_message_manager_t manager, cbus_message_type_t message_type, cbus_message_on_process_func on_message, GPTR opaque);

G_API GErrc cbus_message_manager_unregister(cbus_message_manager_t manager, cbus_message_type_t message_type, cbus_message_id  message_id);
G_API GErrc cbus_message_manager_unregister_category(cbus_message_manager_t manager, cbus_message_type_t message_type);
G_API GErrc cbus_message_manager_unregister_all(cbus_message_manager_t manager);

/** the return cann't do free. */
G_API const cbus_message_handler_t* cbus_message_manager_get_hander(cbus_message_manager_t manager, cbus_message_type_t type, cbus_message_id  message_id);


G_C_HEADER_END



#endif /** !__CBUS_MESSAGE_MANAGER_H__ */

